package com.guaju.yueshangLife.activity;

import android.databinding.DataBindingUtil;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.adapter.BaseFragmentAdapter;
import com.guaju.yueshangLife.base.MyAppcomActivity;
import com.guaju.yueshangLife.databinding.ActivityGoodsDetail2Binding;
import com.guaju.yueshangLife.fragment.ListFragment;
import com.guaju.yueshangLife.viewmodel.GoodsDetailVM;
import com.guaju.yueshangLife.widget.MyCoordinatorLayout;

import java.util.ArrayList;
import java.util.List;

public class GoodsDetailActivity2 extends MyAppcomActivity {
    ActivityGoodsDetail2Binding binding;
    List<Fragment> mFragments;
    Toolbar mToolbar;

    String[]  mTitles=new String[]{
            "商品","详情","评价"
    };
    private MyCoordinatorLayout coordinator;
    private int headerHeight;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         binding=DataBindingUtil.setContentView(this,R.layout.activity_goods_detail2);
         setupViewPager();
         setListener();
         initData();
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void setListener() {
//        coordinator = binding.coordinator;
//        ViewTreeObserver vto = binding.collapsing.getViewTreeObserver();
//        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//            @Override
//            public void onGlobalLayout() {
//                binding.collapsing.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//                headerHeight=binding.collapsing.getHeight();
//                Log.e("GUAJUJERRY", "onGlobalLayout: "+headerHeight );
//                coordinator.setMyScrollChangeListener(new MyCoordinatorLayout.OnScrollChangedListener() {
//                    @Override
//                    public void onMyScrollChanged(int top) {
//                        Log.e("GUAJUJERRY", "onMyScrollChanged: "+top );
//                        if (top>= headerHeight){
//                            binding.centerMenu.setVisibility(View.VISIBLE);
//                        }
//                    }
//                });
//            }
//        });
        binding.collapsing.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View view, int i, int i1, int i2, int i3) {
                Log.e("GUAJUJERRY", "onScrollChange: "+i1);
            }
        });



    }

    private void initData() {
        GoodsDetailVM goodsDetailVM = new GoodsDetailVM();
        binding.setViewModel(goodsDetailVM);
    }

    private void setupViewPager() {
        setupViewPager(binding.viewpager);
    }

    private void setupViewPager(ViewPager viewPager) {
        mFragments=new ArrayList<>();
        for(int i=0;i<mTitles.length;i++){
            ListFragment listFragment = ListFragment.newInstance(mTitles[i]);
            mFragments.add(listFragment);
        }
        BaseFragmentAdapter adapter =
                new BaseFragmentAdapter(getSupportFragmentManager(),mFragments,mTitles);



        viewPager.setAdapter(adapter);
    }
}
